package com.ybfenpan.controller.admin.ti;

import com.ybfenpan.controller.core.BaseController;
import com.ybfenpan.model.log.LogMoneyTi;
import com.ybfenpan.service.log.LogMoneyTiService;
import com.ybfenpan.utils.MapUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by Administrator on 2016/3/25.
 */
@Controller

public class MoneyTiShowController extends BaseController {
    @Resource
    private LogMoneyTiService logMoneyTiService;


    @RequestMapping("admin/ti/logTiShow.shtml")
    public String show(HttpServletRequest request) throws Exception {
        Map formMap = getParameterMap(request);
        int id = MapUtil.getIntValue(formMap, "id");
        if (0 < id) {


            //方法二
            formMap.put("id", id);    //未报单
            List<LogMoneyTi> list = logMoneyTiService.queryForList("logMoneyTiForList", formMap);

            if (list.size() > 0) {
                System.out.println("list.get(0):" + list.get(0));
                //member=list.get(0);
                Integer pageNum = MapUtil.getIntValue(formMap, "pageNum");
                request.setAttribute("pageNum", pageNum);
                request.setAttribute("logTi", list.get(0));
            }

        }
        return "admin/ti/logTiShow";

    }


    @RequestMapping("admin/ti/logTiCommit.shtml")
    public String commit(HttpServletRequest request, ModelMap model) throws Exception {
        Map formMap = getParameterMap(request);
        int id = MapUtil.getIntValue(formMap, "id");
        String startDate = MapUtil.getString(formMap, "startDate");
        String endDate = MapUtil.getString(formMap, "endDate");
        Integer pageNum = MapUtil.getIntValue(formMap, "pageNum");

        String caifuNo = MapUtil.getString(formMap, "caifuNo");
       /* if (caifuNo != null && caifuNo != "") {
            caifuNo = new String(caifuNo.getBytes("iso-8859-1"), "utf-8");
        }*/

        if (0 < id) {
            Map mapLogMoneyTi = new HashMap();
            mapLogMoneyTi.put("id", id);
            mapLogMoneyTi.put("state", 20);
            mapLogMoneyTi.put("commitDate", new Date());
            mapLogMoneyTi.put("caifuNo", caifuNo);

            logMoneyTiService.update(mapLogMoneyTi);

            setMessage(model, "审核成功！");

            formMap.put("id", id);    //未报单
            List<LogMoneyTi> list = logMoneyTiService.queryForList("logMoneyTiForList", formMap);

            if (list.size() > 0) {
                System.out.println("list.get(0):" + list.get(0));
                //member=list.get(0);
                request.setAttribute("logMoneyTi", list.get(0));
            }
        }


        return "redirect:logTiList.shtml?startDate=" + startDate + "&endDate=" + endDate + "&pagination.page=" + pageNum;


    }


    @RequestMapping("admin/ti/logTiCancel.shtml")
    public String cancel(HttpServletRequest request, ModelMap model) throws Exception {
        Map formMap = getParameterMap(request);
        int id = MapUtil.getIntValue(formMap, "id");
        String startDate = MapUtil.getString(formMap, "startDate");
        String endDate = MapUtil.getString(formMap, "endDate");
        Integer pageNum = MapUtil.getIntValue(formMap, "pageNum");

        String caifuNo = MapUtil.getString(formMap, "caifuNo");
//        if (caifuNo != null && !"".equals(caifuNo)) {
//            caifuNo = new String(caifuNo.getBytes("iso-8859-1"), "utf-8");
//        }

        if (0 < id) {

            logMoneyTiService.updateCancel(id, caifuNo);
            setMessage(model, "审核成功！");

            formMap.put("id", id);    //未报单
            List<LogMoneyTi> list = logMoneyTiService.queryForList("logMoneyTiForList", formMap);

            if (list.size() > 0) {
                System.out.println("list.get(0):" + list.get(0));
                //member=list.get(0);
                request.setAttribute("logMoneyTi", list.get(0));
            }
        }
        //return "admin/ti/moneyTiShow";

        return "redirect:logTiList.shtml?startDate=" + startDate + "&endDate=" + endDate + "&pagination.page=" + pageNum;

    }

}
